package part1.language_basis.day06;

import java.util.Arrays;

//求数组元素的最大值，并将最大值放在最后一个元素的下一位置
public class MaxOfArray {
    public static void main(String[] args) {
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random()*100);
            System.out.println(arr[i]);
        }

        /**找数组最大值*/
        int max= arr[0];//假设第1个元素为最大值
        for (int i = 1; i < arr.length; i++) { //遍历剩余元素
            if (arr[i]>max){  //若剩余元素大于max
                max = arr[i]; //将max修改为较大的
            }
        }
        System.out.println("最大值为："+max);
        /**将最大值放在最后一个元素的下一位置*/
        arr = Arrays.copyOf(arr,arr.length+1);
        //System.out.println(Arrays.toString(arr));
        arr[arr.length-1] = max;
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }

        /**找数组最小值*/
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i]<min){
                min = arr[i];
            }
        }
        System.out.println("最小值为："+min);

//        for (int a:arr) {
//            System.out.println(a);
//        }

        /** 冒泡排序 */
        int[] arr2 = new int[10];
        for (int i = 0; i < arr2.length; i++) {
            arr2[i] = (int) (Math.random()*100);
        }
        System.out.println(Arrays.toString(arr2));
        for (int i = 0; i < arr2.length-1; i++) {
            for (int j = 0; j < arr2.length-1-i; j++) {
                if (arr2[j]>arr2[j+1]){
                    int tem = arr2[j];
                    arr2[j] = arr2[j+1];
                    arr2[j+1] = tem;
                }
            }
        }
        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i]);
        }
        System.out.println(Arrays.toString(arr2));
    }
}
